<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>旋转动画</title>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<script src="../lib/webgl-utils.js"></script>
<script src="../lib/webgl-debug.js"></script>
<script src="../lib/cuon-utils.js"></script>
<script src="../lib/cuon-matrix.js"></script>
<script type="x-shader/x-vertex" id="vs">
  attribute vec4 a_Position; //顶点，4个浮点的矢量，attribute变量传输与顶点有关的数据，表示逐顶点的信息
  uniform mat4 u_Matrix; //变换矩阵，4*4浮点矩阵, uniform变量传输的是所有顶点都相同的数据
  void main() {
    gl_Position = u_Matrix*a_Position;
  }
</script>
<script type="x-shader/x-fragment" id="fs">
  precision mediump float; // 精度限定
  uniform vec4 u_FragColor;  // 颜色
  void main() {
    gl_FragColor = u_FragColor;
  }
</script>
<script>
  function main() {
    // 获取 <canvas> 元素
    let canvas = document.getElementById('canvas');
    // 获取WebGL渲染上下文
    let gl = getWebGLContext(canvas);
    if (!gl) {
      console.log('Failed to get the rendering context for WebGL');
      return;
    }

    // 初始化着色器
    if (!initShaders(gl, document.getElementById("vs").text, document.getElementById("fs").text)) {
      console.log('Failed to intialize shaders.');
      return;
    }

    // 设置顶点位置
    let n = initVertexBuffers(gl);
    if (n < 0) {
      console.log('Failed to set the positions of the vertices');
      return;
    }

   // 参数设置
    let ret = initMatrixInfo(gl);
    if (ret < 0) {
      console.log('Failed to set the matrix of the vertices');
      return;
    }

    // 指定清空<canvas>的颜色
    gl.clearColor(0.0, 0.0, 0.0, 1.0);

    // 清空<canvas>
    gl.clear(gl.COLOR_BUFFER_BIT);

    // 绘制三角形
    gl.drawArrays(gl.TRIANGLES, 0, n);
  }

  function initVertexBuffers(gl) {
    let n = 3; // 点的个数
    let vertices = [
      0.25, 0.25,  0,
      -0.25, -0.25,  0,
      0.25, -0.25, 0,
    ];
    // 创建缓冲区对象
    let vertexBuffer = gl.createBuffer();
    if (!vertexBuffer) {
      console.log('Failed to create the buffer object');
      return -1;
    }
    // 将缓冲区对象绑定到目标
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    // 向缓冲区对象写入数据
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
    if (a_Position < 0) {
      console.log('Failed to get the storage location of a_Position');
      return -1;
    }
    gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 0, 0);
    // 连接a_Position变量与分配给它的缓冲区对象
    gl.enableVertexAttribArray(a_Position);

    return n;
  }

  function initMatrixInfo(gl) {
    // 获取 u_FragColor变量的存储地址并赋值
    let u_FragColor = gl.getUniformLocation(gl.program, "u_FragColor");
    if (!u_FragColor) {
      console.log("Failed to get the storage location of u_FragColor");
      return -1;
    }

    // 获取矩阵变量
    let u_Matrix = gl.getUniformLocation(gl.program, "u_Matrix");
    if (!u_Matrix) {
      console.log("Failed to get the storage location of u_Matrix");
      return -1;
    }

    let matrix, angle = 0;

    (function animate() {
      let deg = (Math.PI / 180) * angle++,
              cos = Math.cos(deg),
              sin = Math.sin(deg);

      // 旋转加位移
      matrix = new Float32Array([
        cos, sin, 0.0, 0.5,
        -sin, cos, 0.0, 0.5,
        1.0, 0.0, 0.0, 1.0,
        sin, 0.2, 0.0, 1.5
      ]);

      // v表示可以向着色器传输多个数值(地址变量,webgl中必须false,矩阵)
      gl.uniformMatrix4fv(u_Matrix, false, matrix);

      // 清空画布
      gl.clear(gl.COLOR_BUFFER_BIT);

      // 绘制三角形
      gl.drawArrays(gl.TRIANGLES, 0, 3);

      // 动态设置颜色
      gl.uniform4f(u_FragColor, 1.0, cos, sin, 1.0);

      // 请求动画帧，回调调用自身
      requestAnimationFrame(animate);

      return 0;
    })();
  }
  main();
</script>
</body>
</html>
